﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.教师操作BLL;
using Model;
using System.Configuration;
using NCRE学生考试端V1._0;

namespace NCRE学生考试端V1._0
{
    public partial class frmShowNoScore : Form
    {
        public frmShowNoScore()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 11:51:52
        private static frmShowNoScore FrmShowNoScore = null;
        public static frmShowNoScore GetInstance()
        {
            if (FrmShowNoScore==null|| FrmShowNoScore.IsDisposed)
            {
                FrmShowNoScore = new frmShowNoScore();
            }
            return FrmShowNoScore;
        }
        #endregion

        private Dictionary<string, List<StudentScoreInfo>> dicListStudentBindByIp = null;

        private void frmShowNoScore_Load(object sender, EventArgs e)
        {
            //低分标准,最多输入3个字符
            txtNoSore.MaxLength = 3;
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd hh':'mm tt";

            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd hh':'mm tt";
            bindDataToGridView();
            #region 设置开始时间提前四个小时，截至时间是现在-谷海涛-2018年12月12日15:54:58
            dateTimePicker1.Value = DateTime.Now.AddHours(-4);
            #endregion

        }

        #region 查询低分的方法——陈新月——2018-04-05
        /// <summary>
        /// 查询低分的方法——陈新月——2018-04-05
        /// </summary>
        public void bindDataToGridView()
        {
          
            dicListStudentBindByIp = new Dictionary<string, List<StudentScoreInfo>>();
            StudentBindPaperTypeBLL studentBll = new StudentBindPaperTypeBLL();
            string startTime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endTime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            DateTime starttime1 = Convert.ToDateTime(startTime);
            DateTime endtime2 = Convert.ToDateTime(endTime);
            if (DateTime.Compare(starttime1,endtime2)>=0)
            {
                MessageBox.Show("开始日期不得大于结束日期");
            }
            string Sore = txtNoSore.Text.Trim();

            DataTable dt_studentBind = studentBll.GetPalces(startTime, endTime,Sore);
            if (dt_studentBind == null || dt_studentBind.Rows.Count <= 0)
            {
               
                return;
            }

            int number = 0;
            string Ipc = "";

            foreach (DataRow item in dt_studentBind.Rows)
            {
                number = item["IP"].ToString().LastIndexOf(".");
                if (!item["IP"].ToString().Contains("."))
                {
                    continue;
                }

                Ipc = item["IP"].ToString().ToString().Substring(0, number);
                if (!dicListStudentBindByIp.ContainsKey(Ipc))
                {
                    dicListStudentBindByIp.Add(Ipc, new List<StudentScoreInfo>());
                }

                StudentScoreInfo studentBind = new StudentScoreInfo();
                studentBind.StudentID = item["StudentID"].ToString();
                studentBind.StudentName = item["studentName"].ToString();
                studentBind.PaperType = item["PaperType"].ToString();
                studentBind.Ip = item["IP"].ToString();
                studentBind.Score = item["score"].ToString();
                studentBind.TimeSpan = item["timeSpan"].ToString();
                dicListStudentBindByIp[Ipc].Add(studentBind);
            }

            cmbPlace.DataSource = getPalceByIp().Keys.ToList();
            //如果IP段没有对应的考场，提示信息-谷海涛-2018年12月12日09:37:37
            if (cmbPlace.Items.Count == 0)//此句说明当前iP段没有对应的考场
            {
                MessageBox.Show("请先设置IP对应的考场");
                return;
            }

            // 设定包括Header和所有单元格的列宽自动调整
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            // 设定包括Header和所有单元格的行高自动调整
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            //显示边框
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            //居中显示内容
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            
            dataGridView1.DataSource = dicListStudentBindByIp[getPalceByIp()[cmbPlace.Text]];
            dataGridView1.Columns[0].HeaderText = "学号";
            dataGridView1.Columns[1].HeaderText = "学生姓名";
            dataGridView1.Columns[2].HeaderText = "试卷类型";
            dataGridView1.Columns[3].HeaderText = "IP";
            dataGridView1.Columns[4].HeaderText = "分数";
            dataGridView1.Columns[5].HeaderText = "时间";
            dataGridView1.Columns[0].Width = 150;
            dataGridView1.Columns[1].Width = 150;
            dataGridView1.Columns[2].Width = 150;
            dataGridView1.Columns[3].Width = 150;
            dataGridView1.Columns[4].Width = 150;
        }
        #endregion

        #region 当考场地点发生改变时，查询出对应的信息——陈新月——2018-4-24 09:47:41
        /// <summary>
        /// 当考场地点发生改变时，查询出对应的信息——陈新月——2018-4-24 09:47:41
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = dicListStudentBindByIp[getPalceByIp()[cmbPlace.Text]];

        }
        #endregion

        #region 获取对应IPC段的对应的考场名称
        /// <summary>
        /// 获取对应IPC段的对应的考场名称
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> getPalceByIp()
        {

            IPEntity ipEntity = new IPEntity();
            Dictionary<string, string> dicPlace = new Dictionary<string, string>();
            
            if (dicListStudentBindByIp.Keys.Count > 0)
            {
                for (int i = 0; i < dicListStudentBindByIp.Keys.ToList().Count; i++)
                {
                    string strCon = dicListStudentBindByIp.Keys.ToList()[i].ToString();
                    ipEntity.ipPart = strCon;
                    //从数据库中读出学生上机表的IP数据
                    SelectIpBLL selectIpBLL = new SelectIpBLL();
                    DataTable dt;
                    dt = selectIpBLL.SelectIp(ipEntity);
                    string strPlace = null;
                    if (dt.Rows.Count>0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            strPlace = dt.Rows[j][1].ToString();
                        }
                        dicPlace.Add(strPlace, dicListStudentBindByIp.Keys.ToList()[i].ToString());
                    }

                    
                        
                }
            }
           
            return dicPlace;
        }
        #endregion

        #region 设定文本框的一些限定
        /// <summary>
        /// 设定文本框的一些限定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNoSore_MouseDown(object sender, MouseEventArgs e)
        {
            txtNoSore.SelectionStart = 0;

            txtNoSore.SelectionLength = txtNoSore.Text.Length;

            txtNoSore.SelectAll();
        }
        #endregion

        #region 当日期发生变化时，查询出对应内容——陈新月——2018-4-24 09:48:34
        /// <summary>
        /// 当日期发生变化时，查询出对应内容——陈新月——2018-4-24 09:48:34
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            bindDataToGridView();
        }
        #endregion

        #region 当日期发生变化时，查询出对应内容——陈新月——2018-4-24 09:49:07
        /// <summary>
        /// 当日期发生变化时，查询出对应内容——陈新月——2018-4-24 09:49:07
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            bindDataToGridView();
        }
        #endregion

        #region 当点击查询按钮时，显示查询的内容——陈新月——2018-4-24 09:49:47
        /// <summary>
        /// 当点击查询按钮时，显示查询的内容——陈新月——2018-4-24 09:49:47
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            bindDataToGridView();
        }
        #endregion

        #region 显示低分框中只能输入数字——陈新月——2018-04-05
        /// <summary>
        /// 显示低分框中只能输入数字——陈新月——2018-04-05
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNoSore_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar<48||(int)e.KeyChar>57)&&(int)e.KeyChar!=8 && (int)e.KeyChar!=46)
            {
                e.Handled = true;
                //小数点的处理
                if ((int)e.KeyChar==46)
                {
                    if (txtNoSore.Text.Length<=0)
                    {
                        e.Handled = true;//小数点不能在第一位
                    }
                    else
                    {
                        float f;
                        float oldf;
                        bool b1 = false, b2 = false;
                        b1 = float.TryParse(txtNoSore.Text,out oldf);
                        b2 = float.TryParse(txtNoSore.Text + e.KeyChar.ToString(), out f);
                        if (b2==false)
                        {
                            if (b1==true)
                            {
                                e.Handled = true;
                            }
                            else
                            {
                                e.Handled = false;              
                            }
                        }
                    }
                }
            }
        }
        #endregion

        
    }
}
